package com.example.demo.repository;

import com.example.demo.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author WaterTiger
 * @since 2020/6
 */
@Mapper
@Repository
public interface UserMapper
{
    @Insert("insert into user (username, pwd, iconURL, phone, address, state, times) " +
            "values (#{username},md5(#{pwd}), #{iconURL}, #{phone}, #{address}, #{state}, #{times})")
    public void addUser(
                        @Param("username") String username,
                        @Param("pwd") String pwd,
                        @Param("iconURL") String iconURL,
                        @Param("phone") String phone,
                        @Param("address") String address,
                        @Param("state") int state,
                        @Param("times") String times
                        );

    @Delete("delete from user where id = #{id}")
    public void delUserById(@Param("id") int id);

    @Select("select * from user where id=#{id}")
    public User getUserById(@Param("id") Integer id);

    @Select("select * from user")
    public List<User> getAll();

    @Update("update user set username = #{username}, " +
            "pwd = #{pwd}, " +
            "iconURL = #{iconURL}, " +
            "phone = #{phone}, " +
            "address = #{address}, " +
            "state = #{state}, " +
            "times = #{times} " +
            "where id = #{id}"
    )
    public void saveUser(
            @Param("id") int id,
            @Param("username") String username,
            @Param("pwd") String pwd,
            @Param("iconURL") String iconURL,
            @Param("phone") String phone,
            @Param("address") String address,
            @Param("state") int state,
            @Param("times") String time
    );

    @Select("select id from user where username=#{username} && pwd=md5(#{password})")
    public Integer CheckPassword(@Param("username") String username, @Param("password") String password);


    @Select("select * from user where username=#{username}")
    public User getUserByUserName(String username);
}
